var mclientToken = "token";
var webappurl = window.location.protocol + "//" + window.location.host + "/";

function login(name, password) {
    //window.location.href = 'manager/ajax/ajax.html';
    var islogin = false;
    Vue.http.options.emulateJSON = true;

    var url = webappurl + "login";

    var user = new Object();  //创建params对象
    user.name = name;  //传递参数
    user.password = password;

    var params = JSON.stringify(user);
    console.log(params);
    Vue.http.post(url, params).then(function (data) {
        var result = data.body;

        if (result.status == 1) {
            setClientToken(result.data);
            window.location.href = "querydata.html";
            islogin = true;
        }
        else if (result.status == -1) {
            alert(result.message);
        }
        console.log(result);

    }, function (a, b, c) {

    });
    return islogin;
}

function logout() {
    //window.location.href = 'manager/ajax/ajax.html';

    Vue.http.options.emulateJSON = true;

    var url = webappurl + "logout?token=" + getClientToken();
    Vue.http.post(url, null).then(function (data) {
        var result = data.body;

        console.log(result);
        window.location.href = "index.html";

    }, function (a, b, c) {

    });

}

function vuePost(params, code, successFunction, errorFunction) {

    var url = webappurl + "api/" + code + "?token=" + getClientToken();
    console.log(url);

    // Vue.http.options.emulateJSON = true;
    Vue.http({
        url: url,
        method: "post",
        timeout: 30000, //ms
        body: params,
        emulateJSON: false
    }).then(function (data) {
        if (data) {
            var result = data.body;
            if (result.status == 1) {
                successFunction(result);
            }
            else {
                if (errorFunction != null) {
                    errorFunction();
                }
            }

        } else {

            if (errorFunction != null) {
                errorFunction();
            }
        }
    }, function () {

        if (errorFunction != null) {
            errorFunction();
        }
    });
}

function setClientToken(clientToken) {
    store.session(mclientToken, clientToken);
}

/**
 * 获取token
 * @param firstTime 第一次使用，参数传1
 * @returns
 */
function getClientToken() {

    return store.session.get(mclientToken);

}




